package artid.com.arttogo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.AuctionSpecialInfo_Adapter;
import artid.com.arttogo.bean.AuctionLotListParser;
import artid.com.arttogo.bean.SpecialInfoParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.utils.ToastUtil;
import artid.com.arttogo.view.CircleImageView;
import artid.com.arttogo.view.xlistview.PLA_AdapterView;
import artid.com.arttogo.view.xlistview.XListView;

/**
 * 项目名称：ArtworkArt 类名称：AuctionLotListActivity 类描述：拍品列表、认证拍品  创建人：耿卫斌
 * 创建时间：2017-3-1 下午3:44:42
 */
public class AuctionLotListActivity extends BaseActivity implements
        OnClickListener, XListView.IXListViewListener {
    private ImageView backBtn, searchBtn, specialImg;
    private CircleImageView company_icon;
    private RelativeLayout specialRL;
    private TextView num;
    private LinearLayout noDataLL;
    private View mHeadView;
    private boolean isRefreshed = false;// 是否刷新
    private int page = 1;
    private XListView mListView;
    private AuctionSpecialInfo_Adapter mAdapter;
    private String SpecialID;// 专场ID
    private SpecialInfoParser mSpecialInfoParser;
    private String imgwide;// 请求的图片宽度
    private List<AuctionLotListParser.AuctionLotList_Lotinfo> list = new ArrayList<AuctionLotListParser.AuctionLotList_Lotinfo>();
    private List<AuctionLotListParser.AuctionLotList_Lotinfo> more_list = new ArrayList<AuctionLotListParser.AuctionLotList_Lotinfo>();
    private ArrayList<String> listLotIds=new ArrayList<>();//拍品ID集合，主要是实现可以左右滑动
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lot_list);
        initView();
    }

    private void initView() {
        mContext = AuctionLotListActivity.this;
//		IntentFilter filter = new IntentFilter("com.AuctionSpecialInfo_Activity.receiver");
//		registerReceiver(broadcastReceiver, filter);
        mBack = (ImageView) findViewById(R.id.back_btn);
        mBack.setOnClickListener(this);
        mTittle = (TextView) findViewById(R.id.tittle_text);
        mTittle.setText("认证拍品");
        searchBtn = (ImageView) findViewById(R.id.lot_list_serach);
        searchBtn.setOnClickListener(this);

        imgwide = (Integer.parseInt(NetUtils.getDisplayMetrics((Activity) mContext).get(NetUtils.DM_WIDTH)) / 2) + "";
        mListView = (XListView) findViewById(R.id.AuctionSpecialInfo_listView);
        mListView.setPullRefreshEnable(true);
        mListView.setPullLoadEnable(true);
        mListView.setXListViewListener(this);
        // 头布局
        mHeadView = View.inflate(this, R.layout.auction_lotlist_head, null);

        num = (TextView) mHeadView.findViewById(R.id.lot_list_head);

        noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
        // 头布局放入listView中
        mListView.addHeaderView(mHeadView);
        mListView.setOnItemClickListener(new PLA_AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(PLA_AdapterView<?> parent, View view,
                                    int Position, long id) {
                // TODO Auto-generated method stub
                int position = Position - 2;
                if (position >= 0) {
                    Intent intent = new Intent(mContext, LotDetailPagerActivity.class);
                    intent.putStringArrayListExtra("id", listLotIds);
                    intent.putExtra("index", position);
                    startActivity(intent);
                }
            }

        });
        getLotListData();
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        // 下拉刷新
        page = 1;
        isRefreshed = true;

        getLotListData();
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        // 上拉加载
        page++;
        getLotListData();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.back_btn:
                // 退出
                finish();
                break;
            case R.id.lot_list_serach:
                // 搜索
                Intent intent = new Intent(mContext, LotSearchActivity.class);
                startActivity(intent);
                break;


            default:
                break;
        }
    }


    /**
     * 获取拍品列表 专场id id 页码 page 条数 pagesize 拍品宽度 width 类型 type 默认是1表示专场下的拍品，2认证拍品
     * 用户口令 token 排列顺序 order 当type=2时，排序1.默认 2.成交价
     */
    private void getLotListData() {
        VolleyBasic.getInstance(this).post(
                Cache.auction_LotList,
                VolleyMap.getInstance().auction_LotList(page, 10, "",
                        "2", imgwide, "1"), AuctionLotListParser.class,
                new VolleyCallback() {
                    @SuppressWarnings({"unchecked", "static-access"})
                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        if (isRefreshed) {
                            mListView.stopRefresh();
                        } else {
                            mListView.stopLoadMore();
                        }
                        AuctionLotListParser bean = (AuctionLotListParser) vd
                                .getData();
                        if (page == 1) {
                            num.setText("认证" + bean.getRecordnum() + "件");
                        }
                        list = bean.getLotinfo();
                        if (isRefreshed == true) {
                            more_list.clear();
                            isRefreshed = false;
                        }
                        if (!list.isEmpty()) {
                            more_list.addAll(list);
                        }
                        listLotIds.clear();
                        for (int i = 0; i < more_list.size(); i++) {
                            String idString = more_list.get(i).getId();
                            listLotIds.add(idString);
                        }
                        if (mAdapter == null) {
                            mAdapter = new AuctionSpecialInfo_Adapter(mContext,
                                    more_list);
                            mListView.setAdapter(mAdapter);

                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
                        if (isRefreshed) {
                            mListView.stopRefresh();
                        } else {
                            mListView.stopLoadMore();
                        }
                    }
                });
    }

//	/**
//	 * 广播接收器
//	 */
//	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
//
//		@Override
//		public void onReceive(Context context, Intent intent) {
//			// TODO Auto-generated method stub
//			if (intent.getBooleanExtra("addRemind", false)) {
//				//判断是否为添加提醒的广播通知
//				getSpecialInfoData();
//			};
//		}
//	};

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        // 关闭广播
//		unregisterReceiver(broadcastReceiver);
    }
}
